﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace RegularExpressStudy
{
    class Match1
    {
        public static void Show()
        {
            string originalString = "12ab4ui6uabcy53894uitABRabb";
            string patternString = string.Empty;
            RegexOptions options = RegexOptions.IgnoreCase;
            Regex r = new Regex(patternString, options);

            //1.以下演示是否匹配
            Console.WriteLine("IsMatch演示是否匹配");
            patternString = "ab";
            if (r.IsMatch(originalString))
            {
                Console.WriteLine("Y--字符串{0}能被规则{1}匹配", originalString, patternString);
            }
            else
            {
                Console.WriteLine("N--字符串{0}能不被规则{1}匹配", originalString, patternString);
            }

            //2.使用Match进行匹配
            Console.WriteLine("使用Match进行匹配");
            patternString = @"\d+";
            Match m = Regex.Match(originalString, patternString);
            while (m.Success == true)
            {
                Console.WriteLine(m.Value);
                m = m.NextMatch();
            }

            //3.使用Matches进行多结果集的匹配
            Console.WriteLine("使用Matches进行多结果集的匹配");
            MatchCollection mc = Regex.Matches(originalString, patternString);
            if (mc != null)
            {
                for (int i = 0; i < mc.Count; i++)
                {
                    Console.WriteLine(mc[i].Value);
                }
            }
        }
    }
}
